#-------------------------------------------------------------------------------
# Name:        Bullets
# Purpose:
#
# Author:      Lorien
#
# Created:     03.09.2011
# Copyright:   (c) Lorien 2011
# Licence:     Private
#-------------------------------------------------------------------------------
#!/usr/bin/env python

import pygame
from math import sqrt
from gobject import GObject


#Bullet base-class
class BulletBase(GObject):
    #Game-space bullet w and h - changes via behaviors
    width = 0.5
    height = 0.5

    def __init__(self, map_worker, tower, target):
        # Initing
        GObject.__init__(self, (tower._x, tower._y), map_worker)

        # Initting warfire system
        self.tower = tower
        self.target = target

    def update(self, time_pass):
        x, y = self.x, self.y
        tx, ty = self.target.x, self.target.y
        dx, dy = tx - x, ty - y # got direction vector

        #max move
        max = time_pass * self.speed

        #current distance
        distance = sqrt(dx * dx + dy * dy)

        if distance <= max:
            #here we are!
            pygame.sprite.Sprite.kill(self)
            if self.target.isAlive():
                self.tower.shoot_behavior.shoot(self.tower, self.target)
        else:
            #normalizing and multiply v*t
            dx, dy = dx / distance * self.speed * time_pass, dy / distance * self.speed * time_pass
            #moving bullet
            self.x += dx
            self.y += dy


class BulletSimple(BulletBase):
    def __init__(self, map_worker, tower, target):
        #Initing
        BulletBase.__init__(self, map_worker, tower, target)

        self.speed = 0.04

        #Graphics
        self.loadGraphics('bullet.png')